const sql = require('mssql');
const baseFn = require('../../public/base.js');

// 获取所有申领资产的信息
const getAssetClaimInfo = async (ctx, next) => {
    let getBody = ctx.request.body;
    let searchList = getBody.searchList;
    let selectSql = `SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY T1.id) AS PX,* FROM (SELECT S.SL,S.id,G.assetName+',' as assetName,D.username,
    (SELECT GS.name+'-' FROM GSGL GS WHERE CHARINDEX(CONVERT(VARCHAR, GS.id),D.part)>0  FOR XML PATH('')) AS part,
    (SELECT ZMG.name+'-' FROM ZCMCGL ZMG WHERE CHARINDEX(CONVERT(VARCHAR, ZMG.id),G.assetName)>0  FOR XML PATH('')) AS ZCMC,
    G.classify,G.id AS assetId,S.applyTime,S.BZ,S.returnTime,S.status FROM ZCSL S LEFT JOIN ZCGL G ON S.assetid = G.id LEFT JOIN YG_DA D ON S.userid = D.id) T1 
    WHERE 1= 1  `;
    let totalSql = `SELECT COUNT(T1.id) AS TOTAL FROM (SELECT S.id,G.assetName+',' as assetName,D.username,
    G.classify,S.applyTime FROM ZCSL S LEFT JOIN ZCGL G ON S.assetid = G.id LEFT JOIN YG_DA D ON S.userid = D.id) T1 
    WHERE 1= 1  `;
    if (0 in searchList.assetName) {
        let joinName = searchList.assetName.join(',');
        selectSql += ` AND T1.assetName LIKE '${joinName},%' `;
        totalSql += ` AND T1.assetName LIKE '${joinName},%' `;
    }
    if (searchList.usernam) {
        selectSql += `  AND T1.classify = ${searchList.classify}`;
        totalSql += `  AND T1.classify = ${searchList.classify}`;
    }
    if (searchList.username) {
        selectSql += `  AND T1.username LIKE '%${searchList.username}%'`;
        totalSql += `  AND T1.username LIKE '%${searchList.username}%'`;
    }
    if (0 in searchList.applyTime) {
        selectSql += `  AND (T1.applyTime >= '${searchList.applyTime[0]} 00:00:00' AND T1.applyTime <= '${searchList.applyTime[1]} 23:59:59')`;
        totalSql += `  AND (T1.applyTime >= '${searchList.applyTime[0]} 00:00:00' AND T1.applyTime <= '${searchList.applyTime[1]} 23:59:59')`;
    }
    selectSql += `) AS T2 WHERE T2.PX BETWEEN ${(getBody.pageIndex - 1) * (getBody.pageSize) + 1} AND ${getBody.pageIndex * (getBody.pageSize)}`;
    let result = await sql.query(selectSql);
    let totalResult = await sql.query(totalSql);
    ctx.body = { result: result.recordset, total: totalResult.recordset[0].TOTAL };
}
// 归还资产
const returnAssets = async (ctx, next) => {
    let getBody = ctx.request.body;
    //    把申领的资产状态改成归还
    let changeStatusResult = await sql.query(`UPDATE ZCSL SET status = 'T',returnTime = '${baseFn.getNowTimeId().fullDate}' WHERE id = ${getBody.id}`);
    // 查询可用资产的数量
    let selectCountResult = await sql.query(`SELECT num FROM ZCGL WHERE id = ${getBody.assetId}`);
    // 改变可用资产的数量
    let nowCount = Number(selectCountResult.recordset[0].num) + Number(getBody.SL);
    let changeCountSql = ` UPDATE ZCGL SET num = ${nowCount} WHERE id = ${getBody.assetId}`;
    let changeCountResult = await sql.query(changeCountSql);

    if (changeStatusResult.rowsAffected[0] > 0 && changeCountResult.rowsAffected[0] > 0) {
        ctx.body = {
            success: true
        }
    } else {
        ctx.body = {
            success: false
        }
    }
}


module.exports = {
    'POST /returnAssets': returnAssets,
    'POST /getAssetClaimInfo': getAssetClaimInfo
}